/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.net.greenet.device.td;

import cn.net.greenet.common.db_service.BaseDao;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * Td设备管理类
 *
 * @author ShiYun,2014/12/18
 */
public class TddeviceManageService {

    public static final String QUERY = "SELECT * FROM `shj_db`.`tddevice` WHERE local_ip=? AND local_user=?";
    public static final String QUERYALL = "SELECT * FROM `shj_db`.`tddevice`";
    public static final String INSERT = "INSERT INTO `shj_db`.`tddevice` VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
    public static final String UPDATE = "UPDATE `shj_db`.`tddevice` SET `local_ip` = ? ,`local_port` = ?,`local_user` = ?, `local_pswd` = ?, `freq` = ?, `local_path` = ?,`isRun` = ?,`desc` = ?,`up2_ip` = ?, `up2_port` = ?,`up2_user` = ?,\n"
            + "  `up2_pswd` = ?, `resv1` = ?, `resv2` = ?, `resv3` = ?, `resv4` = ? WHERE `local_ip` = ? AND `local_user` = ?";
    public static final String DELETE = "DELETE FROM `shj_db`.`tddevice` WHERE local_ip=? AND local_user=?";
    private BaseDao dao = null;

    private void init() {
        dao = new BaseDao();
    }

    private void close() {
        if (dao != null) {
            dao.close();
        }
    }

    public boolean insert(TdDevice td) {
        boolean flag = false;
        init();
        Object[] s = {td.getLocal_ip(), td.getLocal_port(), td.getLocal_user(), td.getLocal_pswd(),
            td.getFreq(), td.getLocal_path(), td.getIsRun(), td.getDesc(),
            td.getUp2_ip(), td.getUp2_port(), td.getUp2_user(), td.getUp2_pswd(),
            td.getResv1(), td.getResv2(), td.getResv3(), td.getResv4()};
        int num = dao.executeUpdate(INSERT, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public boolean delete(String ip, String user) {
        boolean flag = false;
        init();
        Object[] s = {ip, user};
        int num = dao.executeUpdate(DELETE, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public boolean update(TdDevice td, String ip, String user) {
        boolean flag = false;
        init();
        Object[] s = {td.getLocal_ip(), td.getLocal_port(), td.getLocal_user(), td.getLocal_pswd(),
            td.getFreq(), td.getLocal_path(), td.getIsRun(), td.getDesc(),
            td.getUp2_ip(), td.getUp2_port(), td.getUp2_user(), td.getUp2_pswd(),
            td.getResv1(), td.getResv2(), td.getResv3(), td.getResv4(), ip, user};
        int num = dao.executeUpdate(UPDATE, s);
        if (num != 0) {
            flag = true;
        }
        close();
        return flag;
    }

    public JSONArray findBYID(String ip, String user) {
        JSONArray one = new JSONArray();
        try {
            init();
            Object[] s = {ip, user};
            ResultSet rs = dao.executeSQL(QUERY, s);
            if (rs.next()) {
                String ip1 = rs.getString("local_ip");
                int port1 = rs.getInt("local_port");
                String local_user = rs.getString("local_user");
                String pswd = rs.getString("local_pswd");
                String freq = rs.getString("freq");
                String local_path = rs.getString("local_path");
                int isRun = rs.getInt("isRun");
                String desc = rs.getString("desc");
                String up2ip = rs.getString("up2_ip");
                int up2port = rs.getInt("up2_port");
                String up2user = rs.getString("up2_user");
                String up2pswd = rs.getString("up2_pswd");
//                String resv1 = rs.getString("resv1");
//                String resv2 = rs.getString("resv2");
//                String resv3 = rs.getString("resv3");
//                String resv4 = rs.getString("resv4");
                one.add(ip1);
                one.add(port1);
                one.add(local_user);
                one.add(pswd);
                one.add(freq);
                one.add(local_path);
                one.add(isRun);
                one.add(desc);
                one.add(up2ip);
                one.add(up2port);
                one.add(up2user);
                one.add(up2pswd);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TddeviceManageService.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            close();
        }
        return one;
    }

    public JSONArray findAll() {
        JSONArray arr = new JSONArray();
        init();
        try {
            String[] s = {};
            ResultSet rs = dao.executeSQL(QUERYALL, s);
            System.out.println("size:" + rs.getRow());
            while (rs.next()) {
                JSONObject one = new JSONObject();
                String ip = rs.getString("local_ip");
                one.put("local_ip", ip);
                int port = rs.getInt("local_port");
                one.put("local_port", port);
                String local_user = rs.getString("local_user");
                one.put("local_user", local_user);
                String pswd = rs.getString("local_pswd");
                one.put("local_pswd", pswd);
                String freq = rs.getString("freq");
                one.put("freq", freq);
                String local_path = rs.getString("local_path");
                one.put("local_path", local_path);
                int isRun = rs.getInt("isRun");
                one.put("isRun", isRun);
                String desc = rs.getString("desc");
                one.put("desc", desc);
                String up2ip = rs.getString("up2_ip");
                one.put("up2_ip", up2ip);
                int up2port = rs.getInt("up2_port");
                one.put("up2_port", up2port);
                String up2user = rs.getString("up2_user");
                one.put("up2_user", up2user);
                String up2pswd = rs.getString("up2_pswd");
                one.put("up2_pswd", up2pswd);
                System.out.println("ip:" + ip + ", port:" + port + ", isRun:" + isRun);
                arr.add(one);
            }
        } catch (SQLException ex) {
            Logger.getLogger(TddeviceManageService.class.getName()).log(Level.SEVERE, null, ex);
        }
        close();
        return arr;
    }
}
